package com.ibigcat.module.mdm.controller.admin.commoditygroup;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.CommonResult;
import com.ibigcat.framework.common.util.object.BeanUtils;
import static com.ibigcat.framework.common.pojo.CommonResult.success;

import com.ibigcat.framework.excel.core.util.ExcelUtils;

import com.ibigcat.framework.apilog.core.annotation.ApiAccessLog;
import static com.ibigcat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ibigcat.module.mdm.controller.admin.commoditygroup.vo.*;
import com.ibigcat.module.mdm.dao.entity.CommodityGroupDO;
import com.ibigcat.module.mdm.service.CommodityGroupService;

@Tag(name = "管理后台 - 组套商品管理")
@RestController
@RequestMapping("/mdm/commodity-group")
@Validated
public class CommodityGroupController {

    @Resource
    private CommodityGroupService commodityGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建组套商品管理")
    @PreAuthorize("@ss.hasPermission('mdm:commodity-group:create')")
    public CommonResult<Long> createCommodityGroup(@Valid @RequestBody CommodityGroupSaveReqVO createReqVO) {
        return success(commodityGroupService.createCommodityGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新组套商品管理")
    @PreAuthorize("@ss.hasPermission('mdm:commodity-group:update')")
    public CommonResult<Boolean> updateCommodityGroup(@Valid @RequestBody CommodityGroupSaveReqVO updateReqVO) {
        commodityGroupService.updateCommodityGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除组套商品管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mdm:commodity-group:delete')")
    public CommonResult<Boolean> deleteCommodityGroup(@RequestParam("id") Long id) {
        commodityGroupService.deleteCommodityGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组套商品管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdm:commodity-group:query')")
    public CommonResult<CommodityGroupRespVO> getCommodityGroup(@RequestParam("id") Long id) {
        CommodityGroupDO commodityGroup = commodityGroupService.getCommodityGroup(id);
        return success(BeanUtils.toBean(commodityGroup, CommodityGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得组套商品管理分页")
    @PreAuthorize("@ss.hasPermission('mdm:commodity-group:query')")
    public CommonResult<PageResult<CommodityGroupRespVO>> getCommodityGroupPage(@Valid CommodityGroupPageReqVO pageReqVO) {
        PageResult<CommodityGroupDO> pageResult = commodityGroupService.getCommodityGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CommodityGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出组套商品管理 Excel")
    @PreAuthorize("@ss.hasPermission('mdm:commodity-group:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCommodityGroupExcel(@Valid CommodityGroupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CommodityGroupDO> list = commodityGroupService.getCommodityGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "组套商品管理.xls", "数据", CommodityGroupRespVO.class,
                        BeanUtils.toBean(list, CommodityGroupRespVO.class));
    }

}